/** 
* @author Riccardo Manfrin [namesurname at gmail dot com]
* 
* @brief  TCP control channel for optional receiver to
* transmitter syncronization.
* 
*/

#include <netinet/in.h>		/* struct sockaddr_in, inet_ntoa */
#include "defines.h"

///Maximum number of pending connection before they are accepted.
#define MAX_PENDING_CONNECTIONS 10

/**
* Function to provide the interface when the IP
* address is not used to specify the server/client,
* but the interface name is used instead.
*/
void ctrl_set_iface(char *iface);

/**
* Function responsible of setting up the 
* TCP server and putting it in listening mode
* at the receiver side.
* It only requires source server address.
*/
int ctrl_srv_setup(struct sockaddr_in *rmtaddr);

/**
* Function responsible of starting the client
* side of the TCP connection for control
* syncrhonization, invoked by the transmitter
* side. This expects a TCP server socket 
* to be up and in listening mode
*/
int ctrl_cnt_setup(struct sockaddr_in *locaddr, struct sockaddr_in *rmtaddr);

/**
* Function responsible of sending data @ client
* side.
* Here we notify the server about the action that is 
* about to be executed on the client side. This is done
* for every single action that is about to be CORRECTLY
* executed on the client: we can't notify the server
* about actions that cannot be taken in charge of by
* the client. That is, a client with no wifi 
* capabilities, for instance, cannot trigger a wireless
* channel modification towards the server, because there
* are no wireless based links to begin with. Some of this
* logic is taken in charge here, while the rest is left
* up the users practical sense.
* On the other hand, we don't deny the possibility that
* a server does not implement all of the client features.
* For instance, if a client has wifi but the server does 
* not, the server can reply that it could not acknowledge
* the related action (and eventually specify the reason 
* why) to the client which shall need to "reconsider"/skip
* the action execution.
*/
int ctrl_cnt_send(char *data, int size);

/**
* Function responsible of holding the client while the 
* server receives the notification and takes appropriate
* actions based on that.
* Basically this is a blocking recv function which parses
* the server reply to know if the action can be executed.
*/
ctrl_ack *ctrl_cnt_wack();

/**
* Function responsible of telling the client about the
* command acceptance on the server.
* The ACK reply message from the server stores the
* following fields
* 
* 0          8         16          24          32
* [-----ACTION_CODE-----][--RESULT--][-REASON_CODE
* -----------]
*/
int ctrl_srv_reply(int fd, char *data, int length);

/**
* Function to know if the control channel is enabled by
* the client.
*/
int ctrl_cnt_cce();

/**
* Function used by the client to set the control channel
* enabled flag.
*/
void ctrl_cnt_set_cce();
